package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.bo.adminbo.*;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketRole;
import com.cskaoyan.app.bean.vo.adminVo.AdminPermissions;
import com.cskaoyan.app.bean.vo.adminVo.AdminRoleOptVo;
import com.cskaoyan.app.exception.ValidationException;
import com.cskaoyan.app.service.AdminRoleService;
import com.cskaoyan.app.util.ValidationUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author kiggy
 * @since 2022/12/29 11:21
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {

    @Autowired
    AdminRoleService adminRoleService;

    @GetMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo){
        // 获得分页信息
        // PageHelper.startPage(1,20);
        PageInfo<MarketRole> adminPageInfo = adminRoleService.listPageAdmins(basePageInfo);

        CommonData<MarketRole> marketRoleCommonData = CommonData.data(adminPageInfo);

        return BaseRespVo.ok(marketRoleCommonData);
    }


    @GetMapping("options")
    public BaseRespVo option(){
        PageInfo<AdminRoleOptVo> pageInfo = adminRoleService.optionRoleList();
        CommonData<AdminRoleOptVo> commonData = CommonData.data(pageInfo);
        return BaseRespVo.ok(commonData);
    }

    @PostMapping("create")
    public BaseRespVo create(@Validated @RequestBody AdminCreateBo createBo, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        // 获得marketRole
        List<MarketRole> marketRole = adminRoleService.createRoleData(createBo);
        return BaseRespVo.ok(marketRole.get(0));
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody AdminDelBo adminDelBo){
        adminRoleService.delRoleData(adminDelBo);
        return BaseRespVo.ok(null);
    }

    /**
     * status = 0 表示正常
     * @param adminBo
     * @return
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody AdminRoleUpdateBo adminBo){

        int status = adminRoleService.update(adminBo);

        return BaseRespVo.ok(null);
    }

    @GetMapping("permissions")
    public BaseRespVo permissions(String roleId){
        AdminPermissions adminPermissions = adminRoleService.selAllPermissions(roleId);

        return BaseRespVo.ok(adminPermissions);
    }

    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody AdminPermissionBo permissionBo){
        String roleId = permissionBo.getRoleId();
        List<String> permissions = permissionBo.getPermissions();
        adminRoleService.updatePermissionData(roleId,permissions);

        return BaseRespVo.ok(null);
    }
}
